import { nextTick, onMounted, ref } from "vue";
import { returnDataObj } from "@/utils/objectUtil";
import { ElLoading, ElMessageBox } from "element-plus";
export const listPageComposition = (compositionData: any) => {
  const t: any = inject("t"),
    list: any = ref([]),
    listLoading = ref(false),
    totalNum = ref(0),
    listQuery: any = ref({}),
    timeType = ref(""),
    searchBoxHeight = ref(0),
    dateValue = ref([]),
    startPlaceholder = ref(""),
    endPlaceholder = ref("");
  // 初始化页面相关数据
  const setInitData = (initData: any) => {
    listQuery.value = Object.assign({}, initData.queryData);
    timeType.value = initData.timeType;
    startPlaceholder.value = initData.startPlaceholder;
    endPlaceholder.value = initData.endPlaceholder;
  };
  // 删除
  const deleteHandler = (id: string) => {
    ElMessageBox.confirm(t("app.confirmDeleteText"), t("app.tipsText"), {
      confirmButtonText: t("app.sureText"),
      cancelButtonText: t("app.cancelText"),
      type: "warning",
      showClose: false,
    }).then(() => {
      const loading = ElLoading.service({
        lock: true,
        text: "Loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      compositionData
        .deleteApi(id)
        .then(() => {
          fetchData("");
          loading.close();
        })
        .catch(() => {
          loading.close();
        });
    });
  };
  // 时间类型选择
  const timeTypeHandler = (val: string) => {
    dateValue.value = [];
    compositionData.timeTypeList.map((time: any) => {
      listQuery.value[time.value + "Start"] = "";
      listQuery.value[time.value + "End"] = "";
      if (time.value === val) {
        timeType.value = val;
        startPlaceholder.value = time.placeholder + "开始时间";
        endPlaceholder.value = time.placeholder + "结束时间";
      }
    });
  };
  // 多条时间选择
  const dateChangeHandler = (val: any) => {
    if (!val || val == null || val === "") {
      compositionData.timeTypeList.map((time: any) => {
        listQuery.value[time.value + "Start"] = "";
        listQuery.value[time.value + "End"] = "";
      });
      return;
    }
    compositionData.timeTypeList.map((time: any) => {
      if (time.value === timeType.value) {
        listQuery.value[time.value + "Start"] = val[0] + " 00:00:00";
        listQuery.value[time.value + "End"] = val[1] + " 23:59:59";
      }
    });
  };
  // 排序
  const sortChangeHandler = (column: any) => {
    if (column.column.columnKey !== undefined) {
      listQuery.value.orderBy = column.column.columnKey;
    }
    listQuery.value.orderDesc = column.order === "ascending" ? "asc" : "desc";
    fetchData("refresh");
  };

  // 页面条数选择
  const pageSizeChange = (val: number) => {
    listQuery.value.pageSize = val;
    fetchData("refresh");
  };
  //   页面跳转
  const pageCurrentChange = (val: number) => {
    listQuery.value.pageNumber = val;
    fetchData("");
  };
  //   搜索
  const handleFilter = () => {
    fetchData("refresh");
  };
  // 数据列表
  const fetchData = (type: string) => {
    if (type === "refresh") listQuery.value.pageNumber = 1;
    const _params = returnDataObj(listQuery.value);
    listLoading.value = true;
    compositionData
      .fetchApi(_params)
      .then((backdata: any) => {
        listLoading.value = false;
        list.value = compositionData.listVo(backdata.data.list);
        totalNum.value = backdata.data.page.totalCount;
      })
      .catch(() => {
        listLoading.value = false;
      });
  };
  onMounted(() => {
    nextTick(() => {
      searchBoxHeight.value = compositionData.searchBoxRef.value?.clientHeight;
    });
  });
  return {
    listQuery,
    listLoading,
    list,
    totalNum,
    timeType,
    startPlaceholder,
    endPlaceholder,
    searchBoxHeight,
    dateValue,
    fetchData,
    setInitData,
    deleteHandler,
    sortChangeHandler,
    pageSizeChange,
    pageCurrentChange,
    handleFilter,
    timeTypeHandler,
    dateChangeHandler,
  };
};
export default listPageComposition;
